//
//  JLFlutterPluginDeviceManager.m
//  flutter_jl_bluetooth_plugin
//
//  Created by 凌煊峰 on 2021/7/28.
//

#import "JLFlutterPluginDeviceManager.h"
#import "JLFlutterBluetoothPluginNotificationConst.h"
#import "JL_EntityToJsonTool.h"

@interface JLFlutterPluginDeviceManager (){
    JL_ManagerM     *mCmdManager;
    DeviceEntity    *currentEntity;
}


@end

@implementation JLFlutterPluginDeviceManager

+ (instancetype)sharedInstance {
    static JLFlutterPluginDeviceManager *instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[self alloc] init];

        [instance addNote];
    });
    return instance;
}

- (void)addNote {
    [JL_Tools add:nil Action:@selector(allNoteListen:) Own:self];
}

#pragma mark - 通知 - 蓝牙设备连接相关
-(void)allNoteListen:(NSNotification*)note{
    NSString *name = note.name;
    
    if ([name isEqual:kFLT_BLE_FOUND]) {
        NSArray *bleArray = [note object];
        self.btEnityList = [NSMutableArray arrayWithArray:bleArray];
        
        if (currentEntity && ![self.btEnityList containsObject:currentEntity]) {
            [self.btEnityList insertObject:currentEntity atIndex:0];
        }
        
        if (self.eventSink) self.eventSink(@{@"EventName" : JLBLEUPDATEDEVICELISTVIEW, @"EventObject": [JL_EntityToJsonTool arrayFromJLEntityList:self.btEnityList]});
    }
    
    if ([name isEqual:kFLT_BLE_PAIRED]) {
        [self.mBLEManager stopScanBLE];
        
        CBPeripheral *pl = [note object];
        for (DeviceEntity *entity in self.btEnityList) {
            if ([entity.mPeripheral isEqual:pl]) {
                currentEntity = entity;
                break;
            }
        }
        
        if (self.eventSink) self.eventSink(@{@"EventName" : JLBLEDEVICECONNECTED, @"EventObject": [JL_EntityToJsonTool arrayFromJLEntityList:self.btEnityList]});
        
        /*--- 获取设备信息 ---*/
        [JL_Tools delay:0.5 Task:^{
            [self preparedDeviceInfo];
        }];
        
    }
    
    if ([name isEqual:kFLT_BLE_CONNECT_FAIL]) {
        currentEntity = nil;
        
        if (self.eventSink) self.eventSink(@{@"EventName" : JLBLEDEVICECONNECTREFUSE, @"EventObject": [NSNumber numberWithBool:YES]});
    }
    
    if ([name isEqual:kFLT_BLE_DISCONNECTED]) {
        currentEntity = nil;
        
        if (self.eventSink){
            self.eventSink(@{@"EventName" : JLBLEUPDATEDEVICELISTVIEW, @"EventObject": [JL_EntityToJsonTool arrayFromJLEntityList:self.btEnityList]});
            
            self.eventSink(@{@"EventName" : JLBLEDEVICEDISCONNECTED, @"EventObject": [NSNumber numberWithBool:YES]});
        }
    }
    
    if ([name isEqual:kFLT_BLE_ON]) {
        
    }
    
    if ([name isEqual:kFLT_BLE_OFF]) {
        currentEntity = nil;
        self.btEnityList = [NSMutableArray new];
        
        if (self.eventSink) self.eventSink(@{@"EventName" : JLBLEUPDATEDEVICELISTVIEW, @"EventObject": [JL_EntityToJsonTool arrayFromJLEntityList:self.btEnityList]});
    }
}


-(void)preparedDeviceInfo{
    mCmdManager = self.mBLEManager.mAssist.mCmdManager;
    
    [mCmdManager cmdTargetFeatureResult:^(NSArray * _Nullable array) {
        JL_CMDStatus st = [array[0] intValue];
        if (st == JL_CMDStatusSuccess) {
            
            JLModel_Device *model = [self->mCmdManager outputDeviceModel];
            
            JL_OtaStatus upSt = model.otaStatus;
            if (upSt == JL_OtaStatusForce) {
                NSLog(@"---> 进入强制升级.");
                [JL_Tools post:kFLT_BLE_OTA Object:nil];
                return;
            }else{
                if (model.otaHeadset == JL_OtaHeadsetYES) {
                    NSLog(@"---> 进入强制升级: OTA另一只耳机.");
                    [JL_Tools post:kFLT_BLE_OTA Object:nil];
                    return;
                }
            }
            NSLog(@"---> 设备正常使用...");
            [JL_Tools mainTask:^{
                /*--- 获取公共信息 ---*/
                [self->mCmdManager cmdGetSystemInfo:JL_FunctionCodeCOMMON Result:nil];
            }];
        }else{
            NSLog(@"---> ERROR：设备信息获取错误!");
        }
    }];
    
}





@end
